package com.tongtech.system.service.impl;

import com.tongtech.common.core.utils.DateUtils;
import com.tongtech.system.domain.*;
import com.tongtech.system.enums.LogStatus;
import com.tongtech.system.mapper.WorkOrderLogMapper;
import com.tongtech.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 工单日志管理 服务实现
 *
 * @author xingyc
 */
@Service
public class WorkOrderLogServiceImpl implements IWorkOrderLogService
{
    @Autowired
    private WorkOrderLogMapper workOrderLogMapper;

    @Autowired
    private IWorkOrderService workOrderService;

    /**
     * 查询工单日志管理数据
     *
     * @param workOrderLog 工单日志信息
     * @return 工单日志信息集合
     */
    @Override
    public List<WorkOrderLog> selectWorkOrderLogList(WorkOrderLog workOrderLog)
    {
        List<WorkOrderLog> list = workOrderLogMapper.selectWorkOrderLogList(workOrderLog);
        if(!CollectionUtils.isEmpty(list)){
            list.stream().forEach(wol -> {
                fillInfo(wol);
            });
        }
        return list;
    }

    private void fillInfo(WorkOrderLog workOrderLog){
        Long projectId = workOrderLog.getProjectId();
    }

    /**
     * 根据工单日志ID查询信息
     *
     * @param workOrderLogId 工单日志ID
     * @return 工单日志信息
     */
    @Override
    public WorkOrderLog selectWorkOrderLogById(Long workOrderLogId)
    {
        WorkOrderLog workOrderLog = workOrderLogMapper.selectWorkOrderLogById(workOrderLogId);
        if(Objects.nonNull(workOrderLog)){
            fillInfo(workOrderLog);
        }
        return workOrderLog;
    }

    /**
     * 新增保存工单日志信息
     *
     * @param workOrderLog 工单日志信息
     * @return 结果
     */
    @Override
    public int insertWorkOrderLog(WorkOrderLog workOrderLog)
    {
        if(Objects.nonNull(workOrderLog)){
            Long workOrderId = workOrderLog.getWorkOrderId();
            if(Objects.nonNull(workOrderId)){
                WorkOrder workOrder = workOrderService.selectWorkOrderById(workOrderId);
                if(Objects.nonNull(workOrder)){
                    Long projectId = workOrder.getProjectId();
                    workOrderLog.setProjectId(projectId);
                }
            }
        }
        int result = workOrderLogMapper.insertWorkOrderLog(workOrderLog);
        return result;
    }

    /**
     * 修改保存工单日志信息
     *
     * @param workOrderLog 工单日志信息
     * @return 结果
     */
    @Override
    public int updateWorkOrderLog(WorkOrderLog workOrderLog)
    {
        int result = workOrderLogMapper.updateWorkOrderLog(workOrderLog);
        return result;
    }

    /**
     * 修改工单日志启动、暂停状态，自动计算问题工时，并累计到工单总工时上。
     *
     * @param workOrderLog 工单日志信息
     * @return 结果
     */
    @Override
    @Transactional
    public int logStatus(WorkOrderLog workOrderLog) {
        String logStatus = workOrderLog.getLogStatus();
        if(LogStatus.STARTED.getCode().equals(logStatus)){
            workOrderLog.setPrevStartTime(new Date());
        }
        if(LogStatus.SUSPENDED.getCode().equals(logStatus) || LogStatus.COMPLATED.getCode().equals(logStatus)){
            //获取数据库中数据
            Long logId = workOrderLog.getLogId();
            WorkOrderLog dbWorkOrderLog = selectWorkOrderLogById(logId);
            Date prevStartTime = dbWorkOrderLog.getPrevStartTime();
            Date prevEndTime = new Date();
            //距离毫秒数
            Long differenceMinutes = DateUtils.getMinutesDifference(prevEndTime, prevStartTime);
            if(Objects.nonNull(differenceMinutes)){
                Float duration = dbWorkOrderLog.getDuration();
                Float totalDuration = duration + differenceMinutes.floatValue();
                workOrderLog.setDuration(totalDuration);
            }
            workOrderLog.setPrevSuspendTime(prevEndTime);
        }
        int result = workOrderLogMapper.updateWorkOrderLog(workOrderLog);
        // TODO 更新工单总时长
        return result;
    }

    /**
     * 删除工单日志管理信息
     *
     * @param deptId 工单日志ID
     * @return 结果
     */
    @Override
    public int deleteWorkOrderLogById(Long deptId)
    {
        return workOrderLogMapper.deleteWorkOrderLogById(deptId);
    }
}
